package com.example.planetexploder.module.picture;

import android.content.Context;
import android.graphics.Bitmap;

import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.bean.maindata.ImgInfo;
import com.example.planetexploder.bean.maindata.ItemInfo;
import com.example.planetexploder.model.dao.Dao;
import com.example.planetexploder.model.folder.Folder;
import com.example.planetexploder.utils.IdCreator;
import com.example.planetexploder.utils.TimeUtil;

import java.util.ArrayList;

public class Presenter_Display {

    private Folder folder;
    private Dao dao;
    private BookInfo bookInfo;
    private ItemInfo itemInfo;

    public Presenter_Display(Context context, BookInfo bookInfo, ItemInfo itemInfo) {
        this.itemInfo = itemInfo;
        folder = new Folder(context);
        this.bookInfo = bookInfo;
        dao = new Dao(context);
    }

    /**
     * 新建图片组
     * @return 图片组信息
     */
    public ItemInfo createNewItem() {
        itemInfo.itemId = IdCreator.create_ImgGroupId();
        itemInfo.itemName = "未命名1";
        itemInfo.dateMod = TimeUtil.getCurrentTime();

        dao.bookTable.add_ItemInfo_To_BookTable(bookInfo.bookId, itemInfo);
        bookInfo.dateMod = TimeUtil.getCurrentTime();
        dao.shelfTable.update_BookInfo(bookInfo);
        dao.imgGroupTable.create_ImgGroupTable(itemInfo.itemId);
        folder.imgFile.create_ImgGroupFolder(bookInfo.bookId,itemInfo.itemId);

        return itemInfo;
    }

    /**
     * 获取图片组里的所有图片
     * @return  所有图片的bitmap数组
     */
    public ArrayList<Bitmap> getImgGroupSorce() {
        return folder.imgFile.get_ImageGroup(bookInfo.bookId, itemInfo.itemId);
    }

    /**
     * 删除图片
     * @param imgId 图片id
     */
    public void delete_Img(String imgId) {
        //删除图片文件
        folder.imgFile.delete_Image_From_ImgGroupFolder(bookInfo.bookId, itemInfo.itemId, imgId);

        String currentTime = TimeUtil.getCurrentTime();

        //更新摘抄项信息
        itemInfo.dateMod = currentTime;
        dao.bookTable.update_ItemInfo(bookInfo.bookId, itemInfo);

        //更新摘抄集信息
        bookInfo.dateMod = currentTime;
        dao.shelfTable.update_BookInfo(bookInfo);
    }


    public ArrayList<ImgInfo> getImgGroupInfo() {
        return dao.imgGroupTable.get_ImgGroupTable(itemInfo.itemId);
    }
}